package com.slfx.easy.poi.demo;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.slfx.easy.poi.entity.Member;
import com.slfx.easy.poi.enums.SexEnum;
import com.slfx.easy.poi.extend.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>简单模板导出</p> <br>
 * 文档：http://doc.wupaas.com/docs/easypoi/easypoi-1c0u6ksp2r091 <br>
 * gitee:  https://gitee.com/lemur/easypoi
 *
 * @author slfx
 */
public class ExcelExportSimpleMember {

    @Test
    public void exportMember() {
        List<Member> dataList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Member member = new Member();
           // member.setName("小明" + i);
            member.setCardNo(String.valueOf(123456789012345678L + i));
            member.setName("小明"+ (i%5 < 3 ? 1 : 2));
            member.setCompanyName("神农庄" + (i%9 < 5 ? 1 : 2));
            member.setAvatar("image/avatar" + (i % 3) + ".jpg");
            member.setBirthday(new Date());
            member.setEmail("zhangsan" + (i) + "@163.com.cn");
            member.setPhoneNumber(String.valueOf(12345678901L + i));
            member.setGender(String.valueOf(i % 2));
            member.setSex(SexEnum.getByCode(i % 2));
            member.setSalary(new BigDecimal(String.valueOf(i + 20000)).divide(new BigDecimal(3), 6, RoundingMode.HALF_UP));
            dataList.add(member);
        }
        Date start = new Date();
        //   ExportParams params = new ExportParams("项目成员", "测试", ExcelType.XSSF);
        ExportParams params = new ExportParams();
        params.setSheetName("项目组成员");
        params.setType(ExcelType.XSSF);
    //    params.setAddIndex(true);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Member.class, dataList);
        try {
            System.out.println("耗时：" + (new Date().getTime() - start.getTime()));
            FileOutputStream fos = new FileOutputStream("D:/poi/easypoi/exportMember.xlsx");
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void exportMemberBigData() {
        Date start = new Date();
        ExportParams params = new ExportParams("项目成员", "大数据测试", ExcelType.XSSF);

        Workbook workbook = ExcelExportUtil.exportBigExcel(params, Member.class, (queryParams, page) -> {
                if (((int) queryParams) == page) {
                    return null;
                }
                List<Object> dataList = new ArrayList<>();
                for (int i = 0; i < 100000; i++) {
                    Member member = new Member();
                    // member.setName("小明" + i);
                    member.setCardNo(String.valueOf(123456789012345678L + i));
                    member.setName("小明"+ (i%5 < 3 ? 1 : 2));
                    member.setCompanyName("神农庄" + (i%9 < 5 ? 1 : 2));
                    member.setAvatar("image/avatar" + (i % 3) + ".jpg");
                    member.setBirthday(new Date());
                    member.setEmail("zhangsan" + (i) + "@163.com.cn");
                    member.setPhoneNumber(String.valueOf(12345678901L + i));
                    member.setGender(String.valueOf(i % 2));
                    member.setSex(SexEnum.getByCode(i % 2));
                    member.setSalary(new BigDecimal(String.valueOf(i + 20000)).divide(new BigDecimal(3), 6, RoundingMode.HALF_UP));
                    dataList.add(member);
                }

                return dataList;
        }, 3);


        try {
            System.out.println("耗时：" + (new Date().getTime() - start.getTime()));
            FileOutputStream fos = new FileOutputStream("D:/poi/easypoi/exportMemberBigData.xlsx");
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
